package ge.devfest.hackathon.gg;

import ge.devfest.hackathon.gg.model.Activity;
import ge.devfest.hackathon.gg.model.DatabaseHelper;
import ge.devfest.hackathon.gg.utils.IBaseActivityFragment;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockFragment;

public abstract class BaseActivityFragment extends SherlockFragment implements IBaseActivityFragment {

	private Activity mActivity;
	private ActivityActivity mActivityActivity;

	@Override
	public void onAttach(android.app.Activity activity) {
		super.onAttach(activity);
		if (!(activity instanceof ActivityActivity)) {
			throw new RuntimeException("fragment: " + getClass() + " is not attached to activity: ActivityActivity");
		}
		mActivityActivity = (ActivityActivity) activity;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mActivity = mActivityActivity.getActivityModel();
		initContentView(mActivity, savedInstanceState);
	}
	
	public ActivityActivity getActivityActivity() {
		return mActivityActivity;
	}
	
	public DatabaseHelper getHelper() {
		return mActivityActivity != null ? mActivityActivity.getHelper() : null;
	}
	
	public Activity getActivityModel() {
		return mActivity;
	}
	
	@Override
	public void onPause() {
		super.onPause();
		saveActivity(mActivity);
	}
	
	@Override
	public void onDetach() {
		super.onDetach();
		mActivityActivity = null;
	}
	
}
